home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / security / xinetd / xinetd.2.0.6 / util.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-22  |  4.0 KB  |  241 lines

  1. /*
  2.  * (c) Copyright 1992 by Panagiotis Tsirigotis
  3.  * All rights reserved.  The file named COPYRIGHT specifies the terms 
  4.  * and conditions for redistribution.
  5.  */
  6.  
  7. static char RCSid[] = "$Id: util.c,v 5.2 1992/11/10 08:18:06 panos Exp $" ;
  8.  
  9. #include <sys/types.h>
  10. /*
  11.  * The following ifdef is for TIOCNOTTY
  12.  */
  13. #ifndef NO_TERMIOS
  14. #include <sys/termios.h>
  15. #else
  16. #include <sys/ioctl.h>
  17. #endif
  18. #include <fcntl.h>
  19.  
  20. #include <memory.h>
  21. #include <syslog.h>
  22. #include <errno.h>
  23. #include <varargs.h>
  24.  
  25. #include "fsma.h"
  26. #include "pset.h"
  27. #include "misc.h"
  28. #include "sio.h"
  29.  
  30. #include "defs.h"
  31. #include "config.h"
  32.  
  33. char *malloc() ;
  34.  
  35. void msg() ;
  36.  
  37.  
  38. void out_of_memory( func )
  39.     char *func ;
  40. {
  41.     msg( LOG_CRIT, func, "ran out of memory" ) ;
  42. }
  43.  
  44.  
  45. struct name_value *nv_find_value( nv_array, name )
  46.     struct name_value nv_array[] ;
  47.     register char *name ;
  48. {
  49.     register struct name_value *nvp ;
  50.  
  51.     for ( nvp = nv_array ; nvp->name ; nvp++ )
  52.         if ( EQ( name, nvp->name ) )
  53.             return( nvp ) ;
  54.     return( NULL ) ;
  55. }
  56.  
  57.  
  58. struct name_value *nv_find_name( nv_array, value )
  59.     struct name_value nv_array[] ;
  60.     register int value ;
  61. {
  62.     register struct name_value *nvp ;
  63.  
  64.     for ( nvp = nv_array ; nvp->name ; nvp++ )
  65.         if ( value == nvp->value )
  66.             return( nvp ) ;
  67.     return( NULL ) ;
  68. }
  69.  
  70.  
  71.  
  72. char **argv_alloc( count )
  73.     unsigned count ;
  74. {
  75.     unsigned argv_size = (count + 1) * sizeof( char *) ;
  76.     char **argv ;
  77.     char *func = "new_argv" ;
  78.  
  79.     argv = (char **) malloc( argv_size ) ;
  80.     if ( argv == NULL )
  81.     {
  82.         out_of_memory( func ) ;
  83.         return( NULL ) ;
  84.     }
  85.     (void) memset( (char *)argv, 0, (int) argv_size ) ;
  86.     return( argv ) ;
  87. }
  88.  
  89.  
  90.  
  91. status_e copy_pset( from, to, allocator )
  92.     pset_h from ;
  93.     pset_h *to ;
  94.     register fsma_h allocator ;
  95. {
  96.     unsigned u ;
  97.     unsigned size ;
  98.     char *func = "copy_pset" ;
  99.  
  100.     if ( *to == NULL )
  101.     {
  102.         *to = pset_create( pset_count( from ), 0 ) ;
  103.         if ( *to == NULL )
  104.         {
  105.             out_of_memory( func ) ;
  106.             return( FAILED ) ;
  107.         }
  108.     }
  109.  
  110.     if ( allocator != NULL )
  111.         size = fsm_size( allocator ) ;
  112.  
  113.     for ( u = 0 ; u < pset_count( from ) ; u++ )
  114.     {
  115.         char *p = (char *) pset_pointer( from, u ) ;
  116.         char *new ;
  117.         
  118.         if ( allocator == NULL )
  119.             new = make_string( 1, p ) ;
  120.         else
  121.             new = fsm_alloc( allocator ) ;
  122.  
  123.         if ( new == NULL )
  124.         {
  125.             out_of_memory( func ) ;
  126.             return( FAILED ) ;
  127.         }
  128.  
  129.         if ( allocator != NULL )
  130.             (void) memcpy( new, p, (int) size ) ;
  131.  
  132.         if ( pset_add( *to, new ) == NULL )
  133.         {
  134.             if ( allocator == NULL )
  135.                 free( new ) ;
  136.             else
  137.                 fsm_free( allocator, new ) ;
  138.             out_of_memory( func ) ;
  139.             return( FAILED ) ;
  140.         }
  141.     }
  142.     return( OK ) ;
  143. }
  144.  
  145.  
  146. void free_pset( pset, allocator )
  147.     pset_h pset ;
  148.     fsma_h allocator ;
  149. {
  150.     register unsigned u ;
  151.     char *p ; 
  152.  
  153.     for ( u = 0 ; u < pset_count( pset ) ; u++ )
  154.     {
  155.         p = (char *) pset_pointer( pset, u ) ;
  156.         if ( allocator == NULL )
  157.             (void) free( p ) ;
  158.         else
  159.             fsm_free( allocator, p ) ;
  160.     }
  161. }
  162.  
  163.  
  164. /*
  165.  * Disassociate from controlling terminal
  166.  */
  167. void no_control_tty()
  168. {
  169.    int fd ;
  170.    char *func = "no_control_tty" ;
  171.  
  172.    if ( ( fd = open( "/dev/tty", O_RDWR ) ) == -1 )
  173.       msg( LOG_WARNING, func, "open of /dev/tty failed: %m" ) ;
  174.    else
  175.    {
  176.       if ( ioctl( fd, TIOCNOTTY, (caddr_t)0 ) == -1 )
  177.          msg( LOG_WARNING, func, "ioctl on /dev/tty failed: %m" ) ;
  178.       (void) close( fd ) ;
  179.    }
  180.    (void) setpgrp( getpid(), 0 ) ;
  181. }
  182.  
  183.  
  184. /*
  185.  * Write the whole buffer to the given file descriptor ignoring interrupts
  186.  */
  187. status_e write_buf( fd, buf, len )
  188.     int fd ;
  189.     char *buf ;
  190.     int len ;
  191. {
  192.     register char *p ;
  193.     register int cc ;
  194.  
  195.     for ( p = buf ; len > 0 ; p += cc, len -= cc )
  196.     {
  197.         cc = write( fd, p, len ) ;
  198.         if ( cc == -1 )
  199.         {
  200.             if ( errno != EINTR )
  201.                 return( FAILED ) ;
  202.             cc = 0 ;
  203.         }
  204.     }
  205.     return( OK ) ;
  206. }
  207.  
  208.  
  209. void tabprint( fd, tab_level, fmt, va_alist )
  210.    int fd ;
  211.    int tab_level ;
  212.    char *fmt ;
  213.    va_dcl
  214. {
  215.    va_list ap ;
  216.    int i ;
  217.  
  218.    for ( i = 0 ; i < tab_level ; i++ )
  219.       Sputchar( fd, '\t' ) ;
  220.  
  221.    va_start( ap ) ;
  222.    Sprintv( fd, fmt, ap ) ;
  223.    va_end( ap ) ;
  224. }
  225.  
  226.  
  227. /*
  228.  * Receive a single IP packet worth of data.
  229.  */
  230. void drain( sd )
  231.    int sd ;
  232. {
  233.    char buf[ DATAGRAM_SIZE ] ;
  234.    char cc ;
  235.  
  236.    cc = recv( sd, buf, sizeof( buf ), 0 ) ;
  237.    if ( cc == -1 )
  238.       msg( LOG_WARNING, "drain", "recv: %m" ) ;
  239. }
  240.  
  241.